package bigraph.editpolicies;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;

import bigraph.commands.AbstractLayoutCommand;
import bigraph.commands.PlaceChangeLayoutCommand;
import bigraph.commands.PlaceCreateCommand;
import bigraph.commands.RootChangeLayoutCommand;
import bigraph.commands.RootCreateCommand;
import bigraph.commands.SiteChangeLayoutCommand;
import bigraph.commands.SiteCreateCommand;
import bigraph.figure.PlaceFigure;
import bigraph.figure.RootFigure;
import bigraph.figure.SiteFigure;
import bigraph.model.Place;
import bigraph.model.Site;
import bigraph.part.CanvasPart;
import bigraph.part.PlacePart;
import bigraph.part.RootPart;
import bigraph.part.SitePart;

public class AppEditLayoutPolicy extends XYLayoutEditPolicy {

	@Override
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		   AbstractLayoutCommand command = null;
		   if (child instanceof RootPart) {
		          command = new RootChangeLayoutCommand();
		   } else if (child instanceof SitePart) {
		          command = new SiteChangeLayoutCommand();
		   } else if (child instanceof PlacePart) {
		          command = new PlaceChangeLayoutCommand();
		   }
		command.setModel(child.getModel());
		command.setConstraint((Rectangle)constraint);
		return command;
	}
	
    protected Command getCreateCommand(CreateRequest request) {
        if (request.getType() == REQ_CREATE && request.getNewObjectType()==Site.class && (getHost() instanceof PlacePart || getHost() instanceof RootPart)) {
               SiteCreateCommand cmd = new SiteCreateCommand();
               cmd.setRoot(getHost().getModel());
               cmd.setSite(request.getNewObject());
               Rectangle constraint = (Rectangle)getConstraintFor(request);
               constraint.x = (constraint.x < 0) ? 0 : constraint.x;
               constraint.y = (constraint.y < 0) ? 0 : constraint.y;
               constraint.width = (constraint.width <= 0) ?
            		   SiteFigure.SITE_FIGURE_DEFWIDTH : constraint.width;
               constraint.height = (constraint.height <= 0) ?
            		   SiteFigure.SITE_FIGURE_DEFHEIGHT : constraint.height;
               cmd.setLayout(constraint);
               return cmd;
        }
        else if (request.getType() == REQ_CREATE && request.getNewObjectType()==Place.class && (getHost() instanceof PlacePart || getHost() instanceof RootPart)) {
            PlaceCreateCommand cmd = new PlaceCreateCommand();
            cmd.setRoot(getHost().getModel());
            cmd.setPlace(request.getNewObject());
            Rectangle constraint = (Rectangle)getConstraintFor(request);
            constraint.x = (constraint.x < 0) ? 0 : constraint.x;
            constraint.y = (constraint.y < 0) ? 0 : constraint.y;
            constraint.width = (constraint.width <= 0) ?
         		   PlaceFigure.PLACE_FIGURE_DEFWIDTH : constraint.width;
            constraint.height = (constraint.height <= 0) ?
         		   PlaceFigure.PLACE_FIGURE_DEFHEIGHT : constraint.height;
            cmd.setLayout(constraint);
            return cmd;
     }
        else if (request.getType() == REQ_CREATE && getHost() instanceof CanvasPart) {
            RootCreateCommand cmd = new RootCreateCommand();
            cmd.setCanvas(getHost().getModel());
            cmd.setRoot(request.getNewObject());
            Rectangle constraint = (Rectangle)getConstraintFor(request);
            constraint.x = (constraint.x < 0) ? 0 : constraint.x;
            constraint.y = (constraint.y < 0) ? 0 : constraint.y;
            constraint.width = (constraint.width <= 0) ?
         		   RootFigure.ROOT_FIGURE_DEFWIDTH : constraint.width;
            constraint.height = (constraint.height <= 0) ?
         		   RootFigure.ROOT_FIGURE_DEFHEIGHT : constraint.height;
            cmd.setLayout(constraint);
            return cmd;
     }
        return null;
   }


}
